package prefix.transaction;

import java.util.Date;

import prefix.duydo.repository.JpaRepository;
import prefix.duydo.repository.Repository;
import prefix.util.JPAUtil;

import models.Transaction;
import models.Transaction.Type;

public class TransactionFactory {

    public final static SetupTransactionFactory setupFactor = new SetupTransactionFactory();

    public final static ThirdPartyFactory tpFactory = new ThirdPartyFactory();

    public final static PayFeeFactory feeFactory = new PayFeeFactory();

    public final static TransferFactory transferFactory = new TransferFactory();

    public static BasicTransaction createTransaction(Type type, Integer initActor, Integer fromActor, Integer toActor,
            Integer fromAccount, Integer toAccount, double amount, Date scheduleDate, Integer orderId, Integer loanId) {

        BasicTransaction trans = caseRule(type);
        Transaction dbTrans = new Transaction(type, initActor, fromActor, toActor, fromAccount, toAccount, amount,
                scheduleDate, orderId, loanId);
        trans.setDbTrans(dbTrans);
        return trans;

    }

    public static BasicTransaction createTransaction(Integer transId) {
        Transaction trans = JPAUtil.em().find(Transaction.class, transId);
        return createTransaction(trans);
    }

    public static BasicTransaction createTransaction(Transaction dbtrans) {
        BasicTransaction abstracTrans = caseRule(dbtrans.getType());
        abstracTrans.setDbTrans(dbtrans);
        return abstracTrans;
    }

    private static BasicTransaction caseRule(Transaction.Type type) {
        switch (type) {
        case T0_ADD_FUNDS:
            return tpFactory.new T0_AddFunds();
        case T1_WITHDRAWL:
            return tpFactory.new T1_LenderWithdrawl();
        case T3_PUSH_FUNDS_TO_BORROWER_BANK:
            return tpFactory.new T3_PUSH_FUNDS_TO_BORROWER_BANK();
        case T4_BORROWER_PAY_ORIG_FEE:
            return feeFactory.new T4_BORROWER_PAY_ORIG_FEE();
        case T5_BORROWER_PAY_MANAGEMENT_FEE:
            return feeFactory.new T5_BORROWER_PAY_MANAGEMENT_FEE();
        case T6_LENDER_PAY_SERVICE_FEE:
            return feeFactory.new T6_LENDER_PAY_SERVICE_FEE();
        case T2_LENDER_ISSUE_FUNDS_TO_BORROWER:
            return transferFactory.new T2_LENDER_ISSUE_FUNDS_TO_BORROWER();
        case T8_BORROWER_MTHLY_PAY_TO_LENDER:
            return transferFactory.new T8_BORROWER_MTHLY_PAY_TO_LENDER();
        default:
            break;
        }
        return null;
    }

}
